<?php
/**
 * Zero_Url_Pattern
 * 
 * Helper Class
 * You should not use this class directly
 * $rules = array('ctl'=>'[a-z_]+', 'act'=>'[a-z_]+', 'action'=>'[a-z_]+', 'tag_id'=>'[\x{4e00}-\x{9fa5}A-Za-z_\-0-9]+'); //tag_id中文
 * 
 * @category   Framework
 * @package    FriendUrl
 * @version    1.0
 * @todo       
 */
/**
 * Zero_Url_PatternManager
 * 
 * It manages patterns of friendly URLs. All the accepted patterns can be stored and 
 * when required can returns the attrubutes of a friendly URL(excluding base URL portion) which best matches wih
 * the stored patterns.
 * Also friendly URL(excluding base URL portion) can be generated based on a stored pattern name and supplied attributes
 * 
 * Where required you should give friendly URL excluding base URL portion
 * 
 * @category   Framework
 * @package    FriendUrl
 * @version    1.0
 * @todo       
 */
class Zero_Url_PatternManager
{    
    private static $_patterns = array();
    private $_subjectUrl = '';
        
    /**
     * Usage:
     * $upm = new Zero_Url_PatternManager();
     */
    function __construct()
    {}
    
    /**
     * Zero_Url_PatternManager::addPattern
     * 
     * It stores a set of url patterns with some rules for matching with a subject
     * and some default values 
     * 
     * Usage:     
     * $pattern = 'ctrlr/:controller/*';
     * $rules = array('controller'=>'[a-z]+', 'action'=>'[a-z]+');
     * $defaults = array('default'=>'test');
     * $upm->addPattern('test1', $pattern, $rules, $defaults);
     * $pattern = 'ctrlr/:controller/acn/:action';
     * $rules = array('controller'=>'[a-z]+', 'action'=>'[a-z]+');
     * $defaults = array('default'=>'test', 'action'=>'acnnn');
     * $upm->addPattern('test2', $pattern, $rules, $defaults);
     * $pattern = 'ctrlr/:controller/acn/:action/aaa/:bbb';
     * $rules = array();
     * $defaults = array('controller'=>'index', 'action'=>'index', 'bbb'=>'ccc');
     * $upm->addPattern('test3', $pattern, $rules, $defaults);
     * $rules = array('ctl'=>'[a-z_]+', 'act'=>'[a-z_]+', 'action'=>'[a-z_]+', 'tag_id'=>'[\x{4e00}-\x{9fa5}A-Za-z_\-0-9]+'); //tag_id中文
     */
    public static function addPattern(/*string*/$name, /*string*/$pattern, /*array of string*/$rules=array(), /*associatve array of string*/$defaults=array())
    {
        $tmp = new Zero_Url_Pattern($pattern, $rules, $defaults);
        self::$_patterns[$name] = $tmp;
        return;
    }
    
    /**
     * Zero_Url_PatternManager::setSubjectUrl
     * 
     * Stores a friendly URL(excluding base URL) which you want to match with the stored patterns 
     * 
     * Usage:
     * $upm->setSubjectUrl('ctrlr/acn/aaa');
     */
    public function setSubjectUrl(/*string*/$url)
    {
        $this->_subjectUrl = $url;
    }
    
    /**
     * Zero_Url_PatternManager::setSubjectUrl
     * 
     * Returns the attributes of the freindly URL stored by Zero_Url_PatternManager::setSubjectUrl
     * 
     * Usage:
     * $upm->setSubjectUrl('ctrlr/index/dsadsad/tyutu/aaaa/bbbb');
     * $attributes = $upm->getAttributes();
     * print_r($attributes);
     * $upm->setSubjectUrl('ctrlr/acn/aaa');
     * $attributes = $upm->getAttributes();
     *  print_r($attributes);
     */
    public function getAttributes()
    {
        $bestMatchWeight = -1;
        $result = false;
        foreach (self::$_patterns as $name=>$pattern)
        {
            $tmp = $pattern->match($this->_subjectUrl, $tm);
            if(!($tmp === false))
            {
                if($tm > $bestMatchWeight)
                {
                    $result = $tmp;
                }                
            }
        }
        return $result;
    }
    
    /**
     * Zero_Url_PatternManager::setSubjectUrl
     * 
     * $pattern  = ':ctl/:act/:met/:typ/*';
     * $pattern  = '*';
     * $rules    = array();
     * $defaults = array('ctl'=>'main', 'act'=>'index', 'met'=>'index', 'typ'=>'e');
     * 
     * $PatternManager = new Zero_Url_PatternManager();
     * $PatternManager->addPattern('default', $pattern, $rules, $defaults);
     * 
     * $supplied_attributes = array('ctl'=>'maaa', '*'=>array('zzzzzz'=>'77777', 'hhhhh'=>'1111', 'iiiii'=>'22222', 'jjjjjj'=>'33333'));
     * $path = $PatternManager->makeUrl('default', $supplied_attributes);
     * 
     * Returns the the freindly URL(excluding base URL)
     */
    public function makeUrl(/*string: pattern name*/$patternName, /*associative array of string*/$attributes)
    {
        if(!isset(self::$_patterns[$patternName]))
        {
            return false;
        }
        $pattern = self::$_patterns[$patternName];
        $result = $pattern->makeUrl($attributes);
        return $result;
    }
}
?>